package com.liangdemo.web.设计模式.单例模式.双重检查;

/**
 * 双重检查
 * 
 * @AUTHOR: Mr.Liang
 * @DATE: 2023/5/6 10:33
 * @DESCRIPTION:
 */
public class SingletonTest01 {}

class Singleton {
    private static volatile Singleton singleton;

    private Singleton() {}

    // 提供一个静态的共有方法，加入双重检查代码，解决线程安全问题，同时解决懒加载问题
    public static synchronized Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}